<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // 1、直接用Array.prototype.flat()
    let arr = [1,[2,3],[4,[5,[6]]]]
    console.log(arr.flat(3)) //参数：提取嵌套层次

    // 2、递归循环
    function flatten(arr){
      var res = []
      for (let x = 0; x < arr.length; x++) {
        if(Array.isArray(arr[x])){
          res = res.concat(flatten(arr[x]))
        }else{
          res.push(arr[x])
        }
      }
      return res
    }
    console.log(flatten(arr))

    // 展开多层不使用递归
    

    // 只展开一层

    // 使用reduce+concat
    console.log(arr)
    var arr1= arr.reduce((account,val)=> account.concat(val),[])
    console.log(arr1)

    // 使用扩展运算符
    console.log(arr)
    var arr2 = [].concat(...arr)
    console.log(arr2)



  </script>
</body>
</html>